package lab;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

public class CenterPanelRear extends JPanel {

    JLabel labelTitle;
    JLabel labelAuthor;
    JLabel labelGenre;
    JLabel labelIsbn;
    JLabel labelYear;
    JLabel labelStock;
    JLabel labelPrice;
    JTextArea textAreaDisc;
    JButton buttonTillVaruKorg;
    ButtonListener buttonListener = new ButtonListener();
    GridBagConstraints c = new GridBagConstraints();
    GridBagLayout gridBagLayout;
    EastPanel eastPanel = new EastPanel();
    String title;
    String price;
    String ISBN;
    ArrayList<String> arrayList = new ArrayList<>();
    ArrayList<String> arrayListISBN=new ArrayList<>();
    int booksSum = 0;
    private String temp;

    public CenterPanelRear(EastPanel eastPanel) {
        this.eastPanel = eastPanel;
        gridBagLayout = new GridBagLayout();
        this.setLayout(gridBagLayout);

        labelTitle = new JLabel("Title: ");
        c.ipady = 10;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.gridx = 1;
        c.gridy = 0;
        add(labelTitle, c);

        labelAuthor = new JLabel("Author: ");
        c.ipady = 10;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.gridx = 4;
        c.gridy = 0;
        add(labelAuthor, c);

        labelGenre = new JLabel("Genre: ");
        c.ipady = 10;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.gridx = 1;
        c.gridy = 1;
        add(labelGenre, c);

        labelIsbn = new JLabel("ISBN: ");
        c.ipady = 10;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.gridx = 4;
        c.gridy = 1;
        add(labelIsbn, c);

        labelYear = new JLabel("Year: ");
        c.ipady = 10;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.gridx = 1;
        c.gridy = 2;
        add(labelYear, c);

        labelStock = new JLabel("Stock: ");
        c.ipady = 10;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.gridx = 4;
        c.gridy = 2;
        add(labelStock, c);

        labelPrice = new JLabel("Price: ");
        c.ipady = 10;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.gridx = 1;
        c.gridy = 3;
        add(labelPrice, c);

        textAreaDisc = new JTextArea();
        textAreaDisc.setEditable(false);
        textAreaDisc.setLineWrap(true);
        textAreaDisc.setOpaque(false);
        textAreaDisc.setBorder(BorderFactory.createEmptyBorder());

        c.ipady = 10;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.weightx = 1;
        c.weighty = 0;
        c.gridheight = 2;
        c.gridx = 1;
        c.gridy = 4;
        add(textAreaDisc, c);

        buttonTillVaruKorg = new JButton("Lägg till varukorg");
        c.ipady = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = GridBagConstraints.HORIZONTAL;
        c.weightx = 0;
        c.weighty = 1.0;
        c.gridx = 5;
        c.gridy = 5;
        add(buttonTillVaruKorg, c);
        buttonTillVaruKorg.addActionListener(buttonListener);
    }

    private class ButtonListener implements ActionListener {

        public ButtonListener() {
        }

        public void actionPerformed(ActionEvent e) {

            if (e.getActionCommand().equals("Lägg till varukorg")) {
                temp = title + "\t" + price;
                arrayListISBN.add(getISBN(ISBN));
                arrayList.add(temp);
                booksSum++;
                String tempString = "";
                for (int i = 0; i < booksSum; i++) {
                    tempString += arrayList.get(i) + "\n";
                }
                eastPanel.textAreaVaruKorg.setText(tempString);
            }
        }
    }

    public String getTitleAndPrice(String title, String price) {
        String s;
        this.title = title;
        this.price = price;
        s = title + price;
        return s;
    }
    public String getISBN(String ISBN){
        this.ISBN=ISBN;
        return ISBN;
    }
}
